package com.example.demo.auth.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.example.demo.auth.entity.UpModule;
import com.example.demo.auth.entity.UpUnit;
import com.example.demo.auth.entity.UpUser;
import com.example.demo.auth.service.UpModuleService;



@Service("upModuleService")
public class UpModuleServiceImpl implements UpModuleService {
	@Autowired
    private JdbcTemplate jdbcTemplate;
	
	public List<UpModule> getAuthModule(UpUser upUser, UpUnit upUnit) {
		
		List<UpModule> moduleList;
        String sql = "SELECT t.NID,t.NPID,t.ID,t.NAME,t.TYPE,t.URL,t.CODE,t.ICON,t.ORDER_NUM,t.DESCRIPTION,t.CREATED_BY,t.CREATION_DATE,t.LAST_UPDATED_BY,t.LAST_UPDATE_DATE,t.ACTIVE_FLAG,t.PARENT_ID,t.IS_DEFAULT,t.IS_ENABLED,t.IS_LEAF " +
                "FROM V_IDM_RESOURCE_AUTHORITY r, V_IDM_MODULE t\n" +
                "WHERE t.ACTIVE_FLAG = 'T' and t.IS_ENABLED='T' AND r.NRESOURCEID = t.NID AND r.NROLEID IN (\n" +
                "  SELECT aru.NROLEID FROM V_IDM_ADMIN_ROLE_USER aru\n" +
                "  WHERE aru.ACTIVE_FLAG = 'T' AND aru.NUSERID = ?  UNION ALL\n" +
                "  SELECT bru.NROLEID FROM V_IDM_BUSI_ROLE_USER bru\n" +
                "  WHERE bru.ACTIVE_FLAG = 'T' AND bru.NUSERID = ? )";
        
        moduleList = jdbcTemplate.queryForList(sql.toString(), UpModule.class,upUser.getNid(),upUser.getNid());
        
        return moduleList;
	}
}
